package sched;

import ibatis.Functions;

import java.util.List;

import models.Times;

public class Main {

	private void usage(){
		System.out.println("Usage: scheduler option");
		System.out.println("Options are:");
		System.out.println("-i --in  [hh:mm:ss]");
		System.out.println("    Check in. You can set the time manually");
		System.out.println("-o --out");
		System.out.println("    Check out");
		System.out.println("-ls");
		System.out.println("    Sets the lunch start time");
		System.out.println("-lf");
		System.out.println("    Sets the lunch finish time");
		System.out.println("-s");
		System.out.println("    Show the especified number of days, ten if no number especified.");
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Main main = new Main();
		
		System.out.println("");
		
		if(args.length == 0 ){
			main.usage();
		}
		else { //at least one argument
			
			String sel = args[0];
			
			if(sel.equals("-i") || sel.equals("--in")){
				if(args.length > 1){ //A check in with time parameter
					//TODO
				}
				else{ //A check in without parameters
					System.out.println("Trying to check you in.");
					if(Functions.insertCheckIn())
						System.out.println("You are succesfully checked in.");
					else //If you are already checked in, you are coming back from dinner
						if(Functions.updateLunchEnd())
							System.out.println("Your lunch finish time was succesfully updated");
						else
							System.out.println("Trying to call Houston. You are having several problems");
				}				
			}
			else if(sel.equals("-o") || sel.equals("--out")){
				System.out.println("Trying to check you out.");
				if(Functions.updateCheckOut())
					System.out.println("You are succesfully checked out.");
			}
			else if(sel.equals("-ls")){
				System.out.println("Are you hungry?");
				if(Functions.updateLunchStart())
					System.out.println("You have been sent to have your dinner.");
			}
			else if(sel.equals("-lf")){
				System.out.println("Do you really wanna start again?.");
				if(Functions.updateLunchEnd())
					System.out.println("Your system have been reactivated without problems.");
			}
			else if(sel.equals("-s") || sel.equals("--show")){
				int numberOfDays = 15;
				if(args.length > 1){ // The number of days are specified
					String numberOfDaysS = args[1];
					try{
						numberOfDays = Integer.parseInt(numberOfDaysS);
					}catch(NumberFormatException e){
						System.out.println("Wrong parameter.");
						System.out.println("Default are goig to be used: 15");
						numberOfDays = 15;
					}
				}
				List<Times> times = Functions.getTimes();
				
				if( times.size() > numberOfDays){ //removing the extra elements
					int n = times.size() - numberOfDays;
					for(int o = 0; o < n; o++){
						times.remove(0);
					}
				}
				
				System.out.println("    DAY         IN         OUT      ");				
				for(Times t : times){
					String comentarios = t.getComentarios();
					if (comentarios == null)
						comentarios = t.getFecha().toString().substring(0, 10);
					String entrada = t.getEntrada();
					String salida = t.getSalida();
					if (salida == null)
						salida = "...";
					System.out.println(comentarios + "   "+ entrada +"   " + salida);
				}
				
			}
			else{ //Not a recognized parameter
				main.usage();
			}			
		}

	}

}
